package com.hll.controller;

import com.alibaba.fastjson.JSONArray;
import com.hll.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Mr.Hu
 * @create: 2021-03-13 16:48
 */
/*@RestController 表示返回的都是 json，不需要加  @ResponseBody*/
@Controller
public class AjaxController {
  @RequestMapping("/a1")
  public void ajax1(@RequestParam("name") String name, HttpServletResponse response)
      throws IOException {
    if ("admin".equals(name)) {
      response.getWriter().print("true");
    } else {
      response.getWriter().print("false");
    }
  }

  @RequestMapping(value = "/a2")
  @ResponseBody
  public String ajax2() {
    List<User> list = new ArrayList<>();
    list.add(new User("秦疆1号", 3, "男"));
    list.add(new User("秦疆2号", 3, "男"));
    list.add(new User("秦疆3号", 3, "男"));
    String json = JSONArray.toJSONString(list);
    return json; // 返回的都是JSON数据，前端不需要转为json对象就能用
    // return "str"; 前端能直接拿到 str
  }
}
